package com.lzhl.buildjar.ui.support;

import com.lzhl.buildjar.dao.DaoFactory;
import com.lzhl.buildjar.util.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2016/3/29
 */
public class Setting {

    public static String getClasspath() {
        String classpath = DaoFactory.getSettingDao().get("classpath");
        if(classpath == null) {
            classpath = "";
        }
        return classpath;
    }

    public static void setClasspath(String classpath) {
        if(classpath == null) {
            return;
        }
        DaoFactory.getSettingDao().update("classpath", classpath);
    }

    public static boolean isShowAdv() {
        String advoption = DaoFactory.getSettingDao().get("showAdv");
        return "true".equals(advoption);
    }

    public static void setShowAdv(boolean showAdv) {
        DaoFactory.getSettingDao().update("showAdv", String.valueOf(showAdv));
    }

    public static Map<String, String> getAutoFill() {
        Set<String> keys = new HashSet<String>();
        keys.add("autoFill_classpath");
        keys.add("autoFill_packageName");
        keys.add("autoFill_className");
        keys.add("autoFill_lastDays");
        keys.add("autoFill_classCount");
        keys.add("autoFill_includeInner");

        Map<String, String> values = DaoFactory.getSettingDao().get(keys);
        if(!values.containsKey("autoFill_classpath")) {
            values.put("autoFill_classpath", "");
        }
        if(!values.containsKey("autoFill_packageName")) {
            values.put("autoFill_packageName", "");
        }
        if(!values.containsKey("autoFill_className")) {
            values.put("autoFill_className", "");
        }
        if(!values.containsKey("autoFill_lastDays")) {
            values.put("autoFill_lastDays", "1");
        }
        if(!values.containsKey("autoFill_classCount")) {
            values.put("autoFill_classCount", "0");
        }
        if(!values.containsKey("autoFill_includeInner")) {
            values.put("autoFill_includeInner", "true");
        }

        return values;
    }

    public static void setAutoFill(
            String classpath, String packageName, String className, int lastDays, int classCount, boolean includeInner) {
        if(lastDays < 0) {
            lastDays = 0;
        }
        if(classCount < 0) {
            classCount = 0;
        }

        Map<String, String> values = new HashMap<String, String>();
        values.put("autoFill_classpath", StringUtils.trim(classpath));
        values.put("autoFill_packageName", StringUtils.trim(packageName));
        values.put("autoFill_className", StringUtils.trim(className));
        values.put("autoFill_lastDays", String.valueOf(lastDays));
        values.put("autoFill_classCount", String.valueOf(classCount));
        values.put("autoFill_includeInner", String.valueOf(includeInner));

        DaoFactory.getSettingDao().update(values);
    }

}
